package com.sywb.chuangyebao.view.dialog;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.ArrayRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sywb.chuangyebao.R;

import org.bining.footstone.adapter.BaseRecyclerAdapter;
import org.bining.footstone.adapter.BaseRecyclerDivider;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.adapter.listener.OnItemClickListener;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/***
 * item选中对话框
 * create by duxl 2018/05/31
 */
public class SelectDialog extends BaseDialogFragment implements OnItemClickListener {

    @BindView(R.id.ll_title_dialog_select)
    LinearLayout llTitle;
    @BindView(R.id.tv_title_dialog_select)
    TextView tvTitle;
    @BindView(R.id.recyclerView_dialog_select)
    RecyclerView mRecyclerView;

    private String title;
    private OnItemClickListener mOnItemClickListener;
    private String[] mItems;
    private @ArrayRes int mItemResid;
    private Object mData;

    public static SelectDialog newInstance(String title) {
        SelectDialog dialogFragment = new SelectDialog();
        Bundle bundle = new Bundle();
        bundle.putSerializable("title", title);
        dialogFragment.setArguments(bundle);
        return dialogFragment;
    }

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.dialog_select;
    }

    @Override
    public void initView(View view, Bundle bundle) {
        if (bundle == null) {
            title = getArguments().getString("title");
        } else {
            title = bundle.getString("title");
        }
        tvTitle.setText(title);
        if(TextUtils.isEmpty(title)) {
            llTitle.setVisibility(View.GONE);
        }

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        BaseRecyclerDivider recyclerDivider = new BaseRecyclerDivider();
        recyclerDivider.setDivider(getResources().getDimensionPixelOffset(R.dimen.dp_1));
        recyclerDivider.setColor(ContextCompat.getColor(getContext(), R.color.line_color));
        mRecyclerView.addItemDecoration(recyclerDivider);

        if(mItemResid != 0) {
            setItems(getResources().getStringArray(mItemResid));
        }

        if(mItems != null && mItems.length > 0) {
            ItemAdapter itemAdapter = new ItemAdapter(getContext(), Arrays.asList(mItems));
            itemAdapter.setOnItemClickListener(this);
            mRecyclerView.setAdapter(itemAdapter);
        }
    }

    @Override
    public void onItemClick(View view, int i) {
        if(mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(this, i);
        }
        exit();
    }

    public class ItemAdapter extends BaseRecyclerAdapter<String> {
        public ItemAdapter(Context context, List<String> items) {
            super(context, R.layout.dialog_select_layout, items);
        }

        @Override
        protected void setItemData(ViewHolderHelper viewHolderHelper, int i, String s) {
            viewHolderHelper.setText(R.id.tvItem_dialog_select_layout, s);
        }
    }

    @OnClick(R.id.tv_cancel_dialog_select)
    @Override
    public void onClick(View v) {
        exit();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putString("title", title);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onResume() {
        super.onResume();
        setWindowHeight();
    }

    private void setWindowHeight() {
        WindowManager.LayoutParams p = getDialog().getWindow().getAttributes();
        p.width = RelativeLayout.LayoutParams.MATCH_PARENT;
        p.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
        getDialog().getWindow().setAttributes(p);
    }

    public void setItems(@ArrayRes int resid) {
        mItemResid = resid;
    }

    public Object getData() {
        return mData;
    }

    public void setData(Object data) {
        this.mData = data;
    }

    public void setItems(String[] items) {
        this.mItems = items;
    }

    public interface OnItemClickListener {
        void onItemClick(SelectDialog dialog, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
